<template>
	<view>
		<page-head title="添加产品">
		</page-head>
		
		<view class="uni-padding-wrap uni-common-mt">
			<form @submit="formSubmit" @reset="formReset">
				<view class="uni-form-item uni-column">
					<view class="title">产品名称</view>
					<input class="uni-input" name="product_name" placeholder="请输入产品名称" />
				</view>
				<view class="uni-form-item uni-column">
					<view class="title">产品分类</view>
					<view class="uni-list">
						<view class="uni-list-cell">
							<view class="uni-list-cell-db">
								<picker @change="bindPickerChange" :value="index" :range="categorylist" range-key="cat_name">
									<view class="uni-input">{{categorylist[index].cat_name}}</view>
								</picker>
							</view>
						</view>
					</view>
				</view>
				<view class="uni-btn-v">
					<button form-type="submit" type="primary">保存</button>
					<button type="default" form-type="reset">重置</button>
				</view>
			</form>
		</view>
	</view>
</template>
<script>
	import graceChecker from "../../../common/graceChecker.js"
	import api from "@/utils/api.js";
	export default {
		data() {
			return {
				index: 0,
				categorylist: [{
					id: 0,
					cat_name: "请选择产品分类"
				}],
				current_category_id: 0
			}
		},
		onLoad() {
			this.fecthCategory();
		},
		methods: {
			async fecthCategory(){
				let { result } = await api.categoryList();
				console.log(result)
				this.categorylist = result.data.list;
			},
			bindPickerChange: function(e) {
				console.log(e)
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.index = e.detail.value
			},
			formSubmit: async function(e) {
				var formData = e.detail.value;
				console.log(formData)
				formData.catgory_id = this.categorylist[this.index].id;
				await api.productAdd(formData);

				uni.switchTab({
					url: "/pages/kucun/product/list"
				})
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}
</style>
